package cn.itcast.controller;


import cn.itcast.entity.Result;

import cn.itcast.util.QiniuUtils;
import com.aliyun.oss.OSSClient;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;

import java.io.IOException;

import java.util.UUID;

/**
 * Created by leiylong on 2020/5/6
 */
@RestController
@RequestMapping("/upload")
public class UploadController {

    @Autowired
    private HttpServletRequest request;

    //上传到本地文件夹，生成图片的随机命名，用nginx服务器展示出来
    @PostMapping("/native2")
    public String nativeUpload2(@RequestParam("file") MultipartFile file) {

        //     File targetFile=null;

        // 文件保存的命名空间
//		String fileSpace = File.separator + "imooc_videos_dev" + File.separator + "mvc-bgm";
        String fileSpace = "D:" + File.separator + "imooc_videos_dev" + File.separator + "images";
        //获取原始文件名
        String originalFilename = file.getOriginalFilename();
        int lastIndexOf = originalFilename.lastIndexOf(".");
        //获取文件后缀
        String suffix = originalFilename.substring(lastIndexOf - 1);
        //使用UUID随机产生文件名称，防止同名文件覆盖
        String fileName = UUID.randomUUID().toString() + suffix;
        System.out.println(fileName);
        String finalPath = fileSpace + File.separator + fileName;
        File createFile = new File(finalPath);
        if (!createFile.getParentFile().exists()) {
            // 创建该文件夹
            createFile.mkdirs();
        }
        try {
            file.transferTo(createFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(fileName);
        System.out.println(file.getOriginalFilename());
        System.out.println(finalPath);

        return "http://localhost:8800/images/" + fileName;

//        if(fileName!=null&&fileName!=""){
//            //获取文件夹路径
//            File file1 =new File(fileSpace);
//            //如果文件夹不存在则创建
//            if(!file1 .exists()  && !file1 .isDirectory()){
//                file1 .mkdir();
//            }
//            //将图片存入文件夹
//            targetFile = new File(file1, fileName);
//
//        }

    }


    //上传图片到tomcat服务器中
    @PostMapping("/native")
    public String nativeUpload(@RequestParam("file") MultipartFile file) {
        String path = request.getSession().getServletContext().getRealPath("/img");
        String wenJianName = file.getOriginalFilename();

        //     String filePath = path +"/"+ file.getOriginalFilename();
        String filePath = path + File.separator + wenJianName;
        File desFile = new File(filePath);
        if (!desFile.getParentFile().exists()) {
            // 创建该文件夹
            desFile.mkdirs();
        }
        try {
            file.transferTo(desFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("path:---" + filePath);
   /*     path:---E:\BaiduNetdiskDownload\
        黑马程序员\30-青橙商城\1.管理后台开发
    \day06\代码\qingcheng_parent\qingcheng_web_manager
    \src\main\webapp\img/property-12.jpg*/
        System.out.println(file.getOriginalFilename());
        return "http://localhost:8080/img/" + file.getOriginalFilename();

    }

    //上传图片到本地
    @PostMapping("/native1")
    public String nativeUpload1(@RequestParam("file") MultipartFile file) {
        String path1 = "D:" + File.separator + "imooc_videos_dev" + File.separator + "images";

        // String filePath1 = path1 +"/"+ file.getOriginalFilename();
        String filePath1 = path1 + File.separator + file.getOriginalFilename();
        File desFile1 = new File(filePath1);
        if (!desFile1.getParentFile().exists()) {
            // 创建该文件夹
            desFile1.mkdirs();
        }
        try {
            file.transferTo(desFile1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("path:---" + filePath1);
   /*     path:---E:\BaiduNetdiskDownload\
        黑马程序员\30-青橙商城\1.管理后台开发
    \day06\代码\qingcheng_parent\qingcheng_web_manager
    \src\main\webapp\img/property-12.jpg*/
        System.out.println(file.getOriginalFilename());
        return "http://localhost:8800/images/" + file.getOriginalFilename();

    }


    @Autowired
    private OSSClient ossClient;

    //    把文件上传到阿里云oss
    @PostMapping("/oss")
    public String ossUpload(@RequestParam("file") MultipartFile file) {
        String bucketName = "lylleiylong";
        //在里面文件夹的名字
        String folder = "ssm";
        String fileName = folder + "/" + UUID.randomUUID() + file.getOriginalFilename();
        try {
            ossClient.putObject(bucketName, fileName, file.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        //第一种方式
        //    return "https://"+bucketName+".oss-cn-shenzhen.aliyuncs.com/"+fileName;
        //第二种方式
        return "http://" + bucketName + "." +
                ossClient.getEndpoint().toString().replace("http://", "") + "/" + fileName;
    }

    //上传文件到七牛云
    @PostMapping("/qiNiu")
    public String qiNiuYunUpload(@RequestParam("file") MultipartFile file) {

        //获取原始文件名
        String originalFilename = file.getOriginalFilename();
        int lastIndexOf = originalFilename.lastIndexOf(".");
        //获取文件后缀
        String suffix = originalFilename.substring(lastIndexOf - 1);
        //使用UUID随机产生文件名称，防止同名文件覆盖
        String fileName = UUID.randomUUID().toString() + suffix;
        try {
            QiniuUtils.upload(file.getBytes(), fileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "http://qa0n3xe6i.bkt.clouddn.com/" + fileName;

    }


}
